<%@ page import="java.sql.*" %><%--
  Created by IntelliJ IDEA.
  User: 12822
  Date: 2023/12/10
  Time: 20:00
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登录判断</title>
</head>
<body>
<%  Connection con;
    String sql;
    ResultSet resultSet;
    PreparedStatement preparedStatement;
    HttpSession httpSession=request.getSession();
    request.setCharacterEncoding("utf8");
    try {
        Class.forName("com.mysql.jdbc.Driver");
        con= DriverManager.getConnection("jdbc:mysql://localhost:3306/it?useUnicode=true&characterEncoding=utf8","root","123456");
        String name=request.getParameter("name"),password=request.getParameter("password");
        System.out.println("登录判断名称"+name);
        sql="select * from login where name= ?";
        preparedStatement=con.prepareStatement(sql);
        preparedStatement.setString(1,name);
        resultSet=preparedStatement.executeQuery();
        boolean login=false;
        while (resultSet.next()){
            String find_password=resultSet.getString("password");
            if(find_password.equals(password)){
                login=true;
                httpSession.setAttribute("name",name);
                break;
            }
        }
        if(login==true) {
            if ("on".equals(request.getParameter("rem_pass"))) {
                System.out.println("设置cookie");
                Cookie cookie=new Cookie("right_login",name);
                cookie.setMaxAge(60*5);
                response.addCookie(cookie);
                Cookie[] cookies=request.getCookies();
                if(cookies!=null) {
                    System.out.println("--------------登录后cookie属性，并加入cookie------------");
                    for (Cookie asd : cookies) {
                        System.out.println(asd.getName() + asd.getValue());
                    }
                }
            }
            response.sendRedirect("shopping.jsp");
        }
        else{
            System.out.println("登录失败");
            response.sendRedirect("login.html");
        }
    } catch (ClassNotFoundException | SQLException e) {
        e.printStackTrace();
        System.out.println("-----登录失败-------");
    }
%>
</body>
</html>
